package com.cat.dataStructure;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/shifting-letters-ii/description/
 * @create 2025/8/24 15:17
 * @since JDK17
 */

public class Solution51 {
    public String shiftingLetters(String s, int[][] shifts) {
        int n = shifts.length, m = s.length(), sum = 0;
        int[] diff = new int[m + 2];
        StringBuilder sb = new StringBuilder(s);
        for (int i = 0; i < n; i++) {
            int t = shifts[i][2] == 1 ? 1 : -1;
            diff[shifts[i][0]] += t;
            diff[shifts[i][1] + 1] -= t;
        }
        for (int i = 0; i < m; i++) {
            sum += diff[i];
            char t = ((char) ((sb.charAt(i) + sum + 26 - 'a') % 26 + 'a'));
            sb.setCharAt(i, t);
        }

        return sb.toString();
    }
}
